import axios from 'axios';

const BASE_URL = 'http://localhost:8080/Yh';

//获取所有发布的优惠券
export const IssuedList = async () => {
    try {
        const response = await axios.get('${BASE_URL}/Issued');
        return response.data;
    } catch (error) {
        console.error('Error listing yh:', error.response ? error.response.data : error.message);
        throw error;
    }
};

//获取所有用户自己的优惠券
export const getCouponByUid = async (id) => {
    try {
        const response = await axios.get(`${BASE_URL}/getCouponByUid/${id}`);
        return response.data;
    } catch (error) {
        console.error('Error listing yh:', error.response ? error.response.data : error.message);
        throw error;
    }
};


//增加用户券信息
export const addYhUser = async (yhUser) => {
    try {
        const response = await axios.post(`${BASE_URL}/addYhUser`, yhUser);
        return response.data;
    } catch (error) {
        console.error('Error add YhUser:', error.response ? error.response.data : error.message);
        throw error;
    }
};

//查询兑换码是否存在
export const selectCode = async (code) => {
    try {
        const response = await axios.get(`${BASE_URL}/selectCode/${code}`);
        return response.data;
    } catch (error) {
        console.error('Error selectCode:', error.response ? error.response.data : error.message);
        throw error;
    }
};

//更新兑换表
export const updateYhCode = async (yhCode) => {
    try {
        const response = await axios.put(`${BASE_URL}/updateYhCode`,yhCode);
        return response.data;
    } catch (error) {
        console.error('Error update YhCode:', error.response ? error.response.data : error.message);
        throw error;
    }
};

//获取用户券通过状态
export const getYhUserByStatus = async (status) => {
    try {
        const response = await axios.get(`${BASE_URL}/getYhUserByStatus/${status}`);
        return response.data;
    } catch (error) {
        console.error('Error get YhUser By Status:', error.response ? error.response.data : error.message);
        throw error;
    }
};

//获取优惠券通过优惠券id
export const getCouponsByCouponId = async (id) => {
    try {
        const response = await axios.get(`${BASE_URL}/getCouponsByCouponId/${id}`);
        return response.data;
    } catch (error) {
        console.error('Error get Coupons By CouponId:', error.response ? error.response.data : error.message);
        throw error;
    }
};

//更新yhUser表
export const updateYhUser = async (yhUser) => {
    try {
        const response = await axios.put(`${BASE_URL}/updateYhUser`,yhUser);
        return response.data;
    } catch (error) {
        console.error('Error update YhUser:', error.response ? error.response.data : error.message);
        throw error;
    }
};